//
//  ViewController.m
//  UI10_作业
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "ViewController.h"
#import "ContactTableView.h"
#import "FirstPushSecondViewController.h"
#import "FirstToThirdViewController.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, ContactDelegate>

/** 联系人数组 */
@property (nonatomic, retain) NSArray *contactArr;

/** 联系人字典 */
@property (nonatomic, retain) NSMutableDictionary *contactDic;
@property (nonatomic, retain) ContactTableView *contactTableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    /** 创建tableView */
    [self createTableView];
    
    /** 数据处理 */
    [self dataHandle];
    
    /** 创建添加联系人按钮 */
    [self creatRightItem];
}

#pragma mark - 创建tableView
- (void)createTableView {
    self.contactTableView = [[ContactTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _contactTableView.backgroundColor = [UIColor cyanColor];
    _contactTableView.dataSource = self;
    _contactTableView.delegate = self;
    [self.view addSubview:_contactTableView];
    [_contactTableView release];
}

#pragma mark - 数据处理
- (void)dataHandle {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160101" ofType:@"plist"];
    /** 联系人字典 */
    self.contactDic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    /** 联系人大小字母的数组（联系人字典的所有的key） */
    self.contactArr = [[self.contactDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
}

#pragma mark - 添加联系人按钮
- (void)creatRightItem {
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightItemAction:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    [rightItem release];
}

- (void)rightItemAction:(UIBarButtonItem *)rightItem {
    FirstPushSecondViewController *firstToSecond = [[FirstPushSecondViewController alloc] init];
    /** 指定代理人 */
    firstToSecond.contactDelegate = self;
    [self.navigationController pushViewController:firstToSecond animated:YES];
    [firstToSecond release];
}

#pragma mark - 实现协议方法
- (void)clickButtonWithName:(NSString *)name phone:(NSString *)phone {
    /** 取名字的第一个字母 */
    NSString *str = [name substringToIndex:1];
    /** 转化成大写字母 */
    NSString *str1 = str.uppercaseString;
            /** 判断大写字母的数组里面是否有这个大写字母 */
        if ([self.contactArr containsObject:str1]) {
            /** 如果有 获取到这个key对应的数组 */
            NSMutableArray *arr = [self.contactDic objectForKey:str1];
            
            /** 创建这个联系人的字典 */
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            /** 添加键值对 名字和电话 */
        
            [dic setValue:name forKey:@"name"];
            [dic setValue:phone forKey:@"phoneNumber"];
            /** 将这个字典添加到数组中 */
            [arr addObject:dic];
#warning 联系人能添加到列表中但是之前的联系人不见了
            [self.contactDic setValue:arr forKey:str1];
            
        } else {
            /** 如果没有 向字典中添加以这个大写字母为key的键值对 */
            /** 创建一个数组作为value */
            NSMutableArray *arr = [NSMutableArray array];
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setValue:name forKey:@"name"];
            [dic setValue:phone forKey:@"phoneNumber"];
            [arr addObject:dic];
#warning 能添加到字典中但是不能显示在列表中
            [self.contactDic setValue:arr forKey:str1];
            
        }
    NSLog(@"%@", self.contactDic);

    self.contactArr = [[self.contactDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    [self.contactTableView reloadData];
    
}

#pragma mark - tableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.contactArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    /** 找到哪个分区 */
    NSString *key = self.contactArr[section];
    /** 每个分区的联系人数组 */
    NSArray *personArr = [self.contactDic objectForKey:key];
    /** 返回每个分区联系人数组的个数 */
    return personArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *pool = @"contactPool";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pool];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:pool];
    }
    /** 找到哪个分区 */
    NSString *key = self.contactArr[indexPath.section];
    /** 找到分区内联系人的数组 */
    NSArray *personArr = [self.contactDic objectForKey:key];
    /** 找到是哪个联系人字典 */
    NSDictionary *personDic = personArr[indexPath.row];
    /** 通过key取到联系人的信息 */
    cell.textLabel.text = personDic[@"name"];
    cell.detailTextLabel.text = personDic[@"phoneNumber"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FirstToThirdViewController *firstToThird = [[FirstToThirdViewController alloc] init];
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    firstToThird.nameStr = cell.textLabel.text;
    firstToThird.phone = cell.detailTextLabel.text;
    [self.navigationController pushViewController:firstToThird animated:YES];
    [firstToThird release];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.contactArr[section];
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.contactArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
